package com.mobileares.midp.widgets.client.page;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-10-29
 * Time: 11:30:30
 * To change this template use File | Settings | File Templates.
 */
public class AccessHelper {
    public static boolean isAccess(HasAccess ha, IFunctionModel functionModel) {
        boolean isAccess = true;
        if (ha.getAccesses() != null) {
            List<AbstractAccess> ands = new ArrayList();
            for (Iterator ir = ha.getAccesses().iterator(); ir.hasNext();) {
                AbstractAccess access = (AbstractAccess) ir.next();
                if (AbstractAccess._OR == access.getRegular()) {
                    if (!access.isAccess(functionModel))
                        isAccess = false;
                    else {
                     return   true;
                    }
                } else if (AbstractAccess._AND == access.getRegular()) {
                    ands.add(access);
                }
            }
            for (AbstractAccess access : ands) {
                if (!access.isAccess(functionModel)) {
                    isAccess = false;
                    break;
                }else{
                    isAccess = true;
                }
            }
        }


        return isAccess;
    }
}
